package com.lagou.rocket.consumer;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.lagou.rocket.entity.Stock;
import com.lagou.rocket.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.swing.*;

@Slf4j
@Component
@RocketMQMessageListener(topic = "tp_springboot_01", consumerGroup = "consumer_grp_03")
public class MyRocketListener implements RocketMQListener<String> {

    @Autowired
    OrderService orderService;

    @Override
    public void onMessage(String message) {
        // 处理broker推送过来的消息
        log.info("rockermq 收到消息，开始下单 >>>>>>>>>,{}",message);

        JSONObject json = JSON.parseObject(message,JSONObject.class);
        Stock stock = JSON.parseObject(json.getString("stock"), Stock.class);
        Integer userId = json.getInteger("userId");
        //创建订单
        orderService.createOrderWithUserInfoInDB(stock, userId);
    }
}
